<template>
  <div>
    <el-col :span="24" style="height: 100%;">
      <div style="height: 100%;">
        <el-row style="height: 150px;padding-top: 50px" type=“flex” align=“middle”>
          <SearchBox></SearchBox>
<!--          <TextareaLimit></TextareaLimit>-->
        </el-row>
        <el-row class="mb8" >
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus"
                       size="mini" @click="handleAdd"
            >新增</el-button
            >
          </el-col>
        </el-row>
        <el-row>
          <el-table
            :max-height="700"
            v-loading="loading" :data="consultList"
            @selection-change="handleSelectionChange"
            stripe border
          >
            <el-table-column label="咨询服务编号" prop="id"
                             :show-overflow-tooltip="true" width="180"
            />
            <el-table-column label="咨询服务地点" prop="location"
                             :show-overflow-tooltip="true" width="250"
            />
            <el-table-column label="咨询开始时间" prop="startTime"
                             :show-overflow-tooltip="true" width="250"
            />
            <el-table-column label="咨询结束时间" prop="endTime"
                             :show-overflow-tooltip="true" width="250"
            />
            <el-table-column label="人数" prop="num"
                             :show-overflow-tooltip="true" width="150"
            />
            <el-table-column label="状态" :show-overflow-tooltip="true"
                             width="150">
              <template slot-scope="scope">
                {{statusList[scope.row.status].label}}
              </template>
            </el-table-column >
            <el-table-column label="操作" align="center"
                             class-name="small-padding fixed-width"
            >
              <div slot-scope="scope" >
                <el-button size="mini" type="text"
                           icon="el-icon-edit" @click="handleUpdate(scope.row)"
                           v-if="scope.row.status === 0"
                >修改</el-button
                >
                <el-button size="mini" type="text"
                           icon="el-icon-delete" @click="handleDelete(scope.row)"
                           v-if="scope.row.status === 0"
                >删除</el-button
                >
                <router-link :to="{path:'/ReservationView',query:{consultId:scope.row.id}}">
                  <el-button size="mini" type="text"
                             v-if="scope.row.status === 1"
                  >预约详情</el-button
                  >
                </router-link>

              </div>
            </el-table-column>
          </el-table>
          <pagination v-show="total > 0"
                      :total="total" :page.sync="queryParams.pageNum"
                      :limit.sync="queryParams.pageSize" @pagination="getPage"
          />

          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">

              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker  format="yyyy-MM-dd HH:mm:ss"  value-format="yyyy-MM-dd HH:mm:ss"
                                 v-model="form.startTime" type="datetime" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker  format="yyyy-MM-dd HH:mm:ss"  value-format="yyyy-MM-dd HH:mm:ss"
                                 v-model="form.endTime" type="datetime" placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="地点" prop="location">
                <el-input v-model="form.location" placeholder="请输入地点" />
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
        </el-row>
      </div>
    </el-col>
  </div>
</template>
<script>
import Vertical from "@/layout/ManageMenu";
import Pagination from "@/components/Pagination";
import SearchBox from "@/views/reservation/search";
import {addConsult, listConsult, pageConsult, updateConsult,deleteConsult} from "@/api/consult";
import TextareaLimit from "@/components/TextareaLimit";
export default {
  name: 'ViewConsult',
  components: {
    TextareaLimit,
    SearchBox,
    Vertical,
    Pagination
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      scoreOpen: false,
      scores: {},

      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        roleName: undefined,
        roleKey: undefined,
        status: undefined,
      },
      // 表单参数
      form: {
        startTime:"",
        endTime:"",
        location:"",
      },
      // 表单校验
      rules: {
        startTime: [{ required: true, message: "开始时间不能为空", trigger: "blur" }],
        endTime: [{ required: true, message: "结束时间不能为空", trigger: "blur" }],
        location: [{ required: true, message: "地点不能为空", trigger: "blur" }],
      },
      defaultProps: {
        children: "children",
        label: "label",
      },

      statusList:[
        {
          id:0,
          label:"待开始"
        },
        {
          id:1,
          label:"已结束"
        },
      ],
    };
  },
  // computed: {
  //   totalPage () { // 总页数
  //     return Math.ceil(this.total / this.pageSize) // 向上取整
  //   },
  //   pageList () { // 获取显示的页码数组
  //     return this.dealPageList(this.currentPage).filter(n => n !== 1 && n !== this.totalPage)
  //   }
  // },
  // watch: {
  //   current (to) { // 当前选中页码
  //     this.currentPage = to
  //   },
  //   currentPage (to) { // 通过更改当前页码，修改分页数据
  //     this.loading = true
  //     this.$emit('change', {
  //       page: to,
  //       pageSize: this.pageSize
  //     })
  //   }
  // },
  created() {
    this.getPage(this.queryParams);
  },
  methods: {
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateConsult(this.form)
              .then((response) => {
                if (response.success) {
                  this.$alert("修改成功", {
                    confirmButtonText: "确认",
                    callback: (action) => {
                      // console.log('请检查后重新输入')
                    },
                  });
                  this.open = false;
                  this.queryParams.pageNum=1
                  this.getPage(this.queryParams)
                } else {
                  throw new Error("出现错误", response.message);
                }
              })
              .catch((e) => {
                this.$alert("出现错误", {
                  confirmButtonText: e + "\n" + "请检查后重新输入",
                  callback: (action) => {
                    console.log("请检查后重新输入");
                  },
                });
              });
          } else {
            addConsult(this.form)
              .then((response) => {
                if (response.success) {
                  this.$alert("新增成功", {
                    confirmButtonText: "确认",
                    callback: (action) => {
                      // console.log('请检查后重新输入')
                    },
                  });
                  this.open = false;
                  this.queryParams.pageNum=1
                  this.getPage(this.queryParams)
                } else {
                  throw new Error("出现错误", response.message);
                }
              })
              .catch((e) => {
                this.$alert("出现错误", {
                  confirmButtonText: e + "\n" + "请检查后重新输入",
                  callback: (action) => {
                    console.log("请检查后重新输入");
                  },
                });
              });
          }
        }
      });
    },
    getList() {
      this.loading = true;
      listConsult().then((response) => {
          if (response.success) {
            this.consultList = response.data;
            this.total = response.data.length;
            this.loading = false;
          }
        }).catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },
    //分页查询
    getPage(data) {
      this.loading = true;
      pageConsult(data)
        .then((response) => {
          if (response.success) {
            this.consultList = response.data.records;
            if(this.total===0){
              this.total = response.data.total;//重要，会决定有几个页码按钮
            }
            this.loading = false;
          }
        })
        .catch((e) => {
          _this.$message.error(e, "出现未知错误");
        });
    },

    // 新增按钮
    handleAdd(){
      this.reset();
      this.open=true;
    },
    handleDelete(row){
      deleteConsult({id:row.id})
        .then((response =>{
          if(response.success){
            this.$alert("删除成功", {
              confirmButtonText: "确认",
              callback: (action) => {
                this.$router.go(0)
              },
            });
          }
        }))
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 修改按钮
    handleUpdate(row) {
      this.reset();
      this.consultList.forEach((item) => {
        if (item.id === row.id) {
          this.form = JSON.parse(JSON.stringify(item));
          this.open = true;
          this.title = "修改咨询";
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        startTime: undefined,
        endTime:undefined,
        location:undefined,
      };
    },

  },
}
</script>

<style lang="scss" scoped>
.auto-generate-form {
  padding-top: 50px;
  padding-right: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
</style>
